package block

import (
	"fmt"
	"os"
	"strconv"
)

type CLI struct {
	//Bc *BlockChain //更新后，cli不用保存区块链实例了，方法自己调用
}

const Usage = `
	01_day_hdv2.exe createBlockChain 地址 			"创建区块链"
	01_day_hdv2.exe addblock "xxxx"					"添加数据xxxx到区块(弃用)"
	01_day_hdv2.exe printChain 						"打印区块链内容"
	01_day_hdv2.exe getBalance 地址 					"获取地址的余额"
	01_day_hdv2.exe send FROM TO AMOUNT MINER DATA	"转账命令(FROM付款人，TO收款人，AMOUNT金额，MINER矿工 DATA区块信息)"
	01_day_hdv2.exe createWallet 					"创建钱包"
	01_day_hdv2.exe listaddress 					"打印所有的钱包地址"
	01_day_hdv2.exe printTx         				"打印所有交易"
`

//给cli提供一个方法，进行命令解析
func (cli *CLI) Run() {
	cmds := os.Args
	if len(cmds) < 2 {
		fmt.Println(Usage)
		os.Exit(1)
	}
	switch cmds[1] {
	case "addblock":

		if len(cmds) != 3 {
			fmt.Println(Usage)
			os.Exit(1)
		}

		fmt.Println("添加数据：", cmds[2], ",到区块中")
		// TODO data:=cmds[2]
		// TODO cli.AddBlock(data)
		break
	case "printChain":
		fmt.Printf("打印区块链所有内容")
		cli.PrintChain()
		break
	case "getBalance":
		fmt.Printf("获取用户余额众")
		cli.GetBalance(cmds[2])
		break
	case "send":
		fmt.Println("正在生成转账，请稍后。")
		from := cmds[2]
		to := cmds[3]
		amount, _ := strconv.ParseFloat(cmds[4], 64) //把字符串转为float类型
		miner := cmds[5]
		data := cmds[6]
		cli.Send(from, to, amount, miner, data)
		break
	case "createBlockChain":
		cli.CreateBlockChain(cmds[2])
		break
	case "createWallet":
		cli.CreateWallet()
		break
	case "listaddress":
		cli.ListAddress()
		break
	case "printTx":
		fmt.Println("打印所有交易")
		cli.PrintTx()
	default:
		fmt.Println(Usage)
		os.Exit(1)
	}
}
